//
//  PKPlayMainViewController.m
//  PianKe
//
//  Created by lanou on 15/12/25.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKPlayMainViewController.h"

#import "PKMusicPlayer.h"


// 遵循list控制器的代理,
@interface PKPlayMainViewController () <ChangeMusicDelegate>



@property (strong, nonatomic) UIScrollView *mainScrollView;
@property (strong, nonatomic) UIWebView *webView;
@property (strong, nonatomic) PKMusicPlayer *player;

@end

@implementation PKPlayMainViewController

-(void)dealloc
{
    // 返回的时候,把tableView上的model设置为为选中状态
    PKDetailModel *currentModel = _listArray[_player.index];
    currentModel.isSelected = NO;
    [_listTableVC.tableView reloadData];

     NSLog(@"xiaohuila");
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    // 添加自定义导航栏
    [self addBarView];
    
    
    
    // 创建播放器
    _player = [PKMusicPlayer sharePlayer];
    
    NSMutableArray *array = [NSMutableArray array];
    for (PKDetailModel *model  in _listArray) {
        // 创建数组,获取所有的音频地址
        [array addObject:model.musicUrl];
    }
    // 播放音乐 : 播放前面点击的index对应的歌曲
    _player.musicArray = array;
    [_player changeMusicWithIndex:self.firstIndex];
    
    
    // 添加3个页面的scrollView
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, kScreenWidth, kScreenHeight - 70 - 90)];
    _mainScrollView.contentSize = CGSizeMake(kScreenWidth * 3, _mainScrollView.height);
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.showsVerticalScrollIndicator = NO;
    _mainScrollView.pagingEnabled = YES;
    [self.view addSubview:_mainScrollView];
    
    
#pragma mark  ------ 第一页的tabelView
    _listTableVC = [[PKPlayListTableViewController alloc] init];
     // 传值:
    _listTableVC.listArray = _listArray;
    
    // 设置代理属性
    _listTableVC.changeDelegate = self;

    _listTableVC.tableView.frame = CGRectMake(0, 0, kScreenWidth, _mainScrollView.height);
    [_mainScrollView addSubview:_listTableVC.tableView];
    
    
#pragma mark  ------  第二页播放电台
    _infoVC = [[PKPlayInfoViewController alloc] init];
    _infoVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, _mainScrollView.height);
    _infoVC.view.backgroundColor = [UIColor whiteColor];
    [_mainScrollView addSubview:_infoVC.view];
  
    
#pragma mark  ------  第三页网页加载
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(kScreenWidth * 2, 0, kScreenWidth, _mainScrollView.height)];
    [_mainScrollView addSubview:_webView];
    

    
    
#pragma mark  ------  底部的播放栏
    // 上一首歌曲的按钮
    UIButton *beforeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    beforeButton.frame = CGRectMake(20, kScreenHeight - 20 - 20, 20, 20) ;
    beforeButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:beforeButton];
    
    // 下一首歌曲的按钮
    UIButton *nextButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    nextButton.frame = CGRectMake(kScreenWidth - 20 - 20, beforeButton.top, 20, 20);
    nextButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:nextButton];
    
    // 播放按钮
    UIButton *playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    playButton.frame = CGRectMake((kScreenWidth - 20 * 4 - 40) / 2 + beforeButton.right, beforeButton.top, 30, 30);
    playButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:playButton];
    
    [beforeButton addTarget:self action:@selector(before:) forControlEvents:(UIControlEventTouchUpInside)];
    
     [nextButton addTarget:self action:@selector(next:) forControlEvents:(UIControlEventTouchUpInside)];
    
     [playButton addTarget:self action:@selector(play:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    // 通过Detail页面传来的firstIndex来修改全部的信息
    [self changeAllViewsWithIndex:_firstIndex];
    
    [_listTableVC.tableView reloadData];
}


- (void)addBarView
{
    // 创建自定义导航栏
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [self.view addSubview:headView];
    
    UIButton *leftBack = [UIButton buttonWithType:(UIButtonTypeCustom)];
    leftBack.frame = CGRectMake(10, 30, 30, 30);
    [leftBack setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:(UIControlStateNormal)];
    [headView addSubview:leftBack];
    [leftBack addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];

}



// 实现返回
- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
    
}



// 上一首歌曲的按钮方法实现
- (void)before:(UIButton *)button
{
    
    // 取出当前的播放的model , 而我们传进来的是我们点击的model
    PKDetailModel *currentModel = _listArray[_player.index];
    currentModel.isSelected = NO; // 把点选状态改为NO
    
    // 先播放下一曲
    [_player beforeMusic];
     // 然后把此时播放器播放的歌的下标,传给改变所有页面的信息的方法
    [self changeAllViewsWithIndex:_player.index];
    
    [_listTableVC.tableView reloadData];
    
}

// 播放按钮方法
- (void)play:(UIButton *)button
{
    
    if (_player.isPlaying) {
        // 说明在播放
        [_player pause];
    } else
    {
        [_player play];
    }
    
}

// 下一首歌曲的按钮实现方法
- (void)next:(UIButton *)button
{
    
    // 取出当前的播放的model , 而我们传进来的是我们点击的model
    PKDetailModel *currentModel = _listArray[_player.index];
    currentModel.isSelected = NO; // 把点选状态改为NO
    
    
    // 先播放下一曲
    [_player nextMusic];
    // 然后把此时播放器播放的歌的下标,传给改变所有页面的信息的方法
    [self changeAllViewsWithIndex:_player.index];
    
   
}







// 修改所有页面的信息
- (void)changeAllViewsWithIndex:(NSInteger)index
{
    
    // 取出当前的播放的model , 而我们传进来的是我们点击的model
    PKDetailModel *currentModel = _listArray[_player.index];
    currentModel.isSelected = NO; // 把状态改为NO
    
    
    // 1.改变的音乐
    [_player changeMusicWithIndex:index];
    
    // 2.改变的信息
    PKDetailModel *model = _listArray[index];
    [_infoVC setViewWithListDetailModel:model];
    
    // 3.改变webView的具体信息
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.webview_url]]];
    
    // 4.改变list的样式
    // 取出点击的model  (上一曲,下一曲的按钮方法实现中,currentModel 和 selectModel是同一个model)
    PKDetailModel *selectModel = _listArray[index];
    // 修改状态
    selectModel.isSelected = YES;
     
    
    // 刷新
    [_listTableVC.tableView reloadData];

    
}



// 实现ChangeMusicDelegate代理方法
- (void)changeWithIndex:(NSInteger)index
{
    // 通过index来修改
    
    // 然后修改全部页面的信息,就是调用上面的方法
    [self changeAllViewsWithIndex:index];
    
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
